//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using System;
using System.ComponentModel.Design;
using System.Configuration;
using System.Windows.Forms;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;

namespace EntLibContrib.Resource.Configuration.Design
{
	/// <summary>
	/// Represents the design manager that will register the design time information for the Resource Application Block.
	/// </summary>
	public sealed class ResourceConfigurationDesignManager : ConfigurationDesignManager
	{
		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="ResourceConfigurationDesignManager"/> class.
		/// </summary>
		public ResourceConfigurationDesignManager ()
		{
		}
		#endregion

		#region Public Methods
		/// <summary>
		/// Registers the resource design manager into the environment.
		/// </summary>
		/// <param name="serviceProvider">The a mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</param>
		public override void Register (IServiceProvider serviceProvider)
		{
			ResourceNodeMapRegistrar nodeRegistrar = new ResourceNodeMapRegistrar(serviceProvider);
			nodeRegistrar.Register();
			ResourceCommandRegistrar cmdRegistrar = new ResourceCommandRegistrar(serviceProvider);
			cmdRegistrar.Register();
		}
		#endregion

		#region Protected Methods
		/// <summary>
		/// Opens the resource configuration from an application configuration file.
		/// </summary>
		/// <param name="serviceProvider">A mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</param>
		/// <param name="rootNode">The <see cref="ConfigurationApplicationNode"/> of the hierarchy.</param>
		/// <param name="section">The resource configuration section or null if no section was found.</param>
		protected override void OpenCore (IServiceProvider serviceProvider, ConfigurationApplicationNode rootNode, ConfigurationSection section)
		{
			if ( null != section )
			{
				ResourceManagerSettingsNodeBuilder builder = new ResourceManagerSettingsNodeBuilder(serviceProvider, (ResourceManagerSettings)section);
				ResourceManagerSettingsNode resourceManagerNode = builder.Build();
				SetProtectionProvider(section, resourceManagerNode);

				rootNode.AddNode(resourceManagerNode);
			}
		}

		/// <summary>
		/// Gets a <see cref="ConfigurationSectionInfo"/> object containing the Resource Block's configuration information.
		/// </summary>
		/// <param name="serviceProvider">A mechanism for retrieving a service object; that is, an object that provides custom support to other objects.</param>
		/// <returns>A <see cref="ConfigurationSectionInfo"/> object containing the Resource Block's configuration information.</returns>
		protected override ConfigurationSectionInfo GetConfigurationSectionInfo (IServiceProvider serviceProvider)
		{
			ConfigurationNode rootNode = ServiceHelper.GetCurrentRootNode(serviceProvider);
			ResourceManagerSettingsNode node = null;
			if ( rootNode != null ) node = (ResourceManagerSettingsNode)rootNode.Hierarchy.FindNodeByType(rootNode, typeof(ResourceManagerSettingsNode));
			ResourceManagerSettings resourceSection = null;
			if ( node == null )
			{
				resourceSection = null;
			}
			else
			{
				ResourceManagerSettingsBuilder builder = new ResourceManagerSettingsBuilder(serviceProvider, node);
				resourceSection = builder.Build();
			}
			string protectionProviderName = GetProtectionProviderName(node);

			return new ConfigurationSectionInfo(node, resourceSection, ResourceManagerSettings.SectionName, protectionProviderName);
		}
		#endregion
	}
}